package file

import (
	"io"
	"os"

	"gitee.com/injoyai/goutil/bytes"
)

type Data struct {
	name  string
	isDir bool
	*bytes.Buffer
}

func NewData(name string, bs []byte, isDir ...bool) *Data {
	return &Data{
		name:   name,
		Buffer: bytes.NewBuffer(bs),
		isDir:  len(isDir) > 0 && isDir[0],
	}
}

func NewDataFile(file *os.File) (*Data, error) {
	info, err := file.Stat()
	if err != nil {
		return nil, err
	}
	name := info.Name()
	if len(name) == 0 {
		name = file.Name()
	}
	data := &Data{name: name, isDir: info.IsDir()}
	_, err = io.Copy(data, file)
	return data, err
}

func (this *Data) Name() string {
	return this.name
}

func (this *Data) IsDir() bool {
	return this.isDir
}
